(function($) {
    var arkflow = $.arkflow;

    //流程工具方法
    arkflow.util = {
        nextId : (function() {
            var c = 0;
            return function() {
                return ++c;
            };
        })(),

        connPoint : function(fromRect, endPoint) {
            var fromRectCenterPoint = {
                x : fromRect.x + fromRect.width / 2,
                y : fromRect.y + fromRect.height / 2
            };
            var tanDyx = (fromRectCenterPoint.y - endPoint.y) / (fromRectCenterPoint.x - endPoint.x);
            tanDyx = isNaN(tanDyx) ? 0 : tanDyx;
            var rectDyx = fromRect.height / fromRect.width;
            var upY = endPoint.y < fromRectCenterPoint.y ? -1 : 1;
            var upX = endPoint.x < fromRectCenterPoint.x ? -1 : 1;
            var connPointY;
            var connPointX;
            if (Math.abs(tanDyx) > rectDyx && upY == -1) {
                connPointY = fromRectCenterPoint.y - fromRect.height / 2;
                connPointX = fromRectCenterPoint.x + upY * fromRect.height / 2 / tanDyx;
            } else {
                if (Math.abs(tanDyx) > rectDyx && upY == 1) {
                    connPointY = fromRectCenterPoint.y + fromRect.height / 2;
                    connPointX = fromRectCenterPoint.x + upY * fromRect.height / 2 / tanDyx;
                } else {
                    if (Math.abs(tanDyx) < rectDyx && upX == -1) {
                        connPointY = fromRectCenterPoint.y + upX * fromRect.width / 2 * tanDyx;
                        connPointX = fromRectCenterPoint.x - fromRect.width / 2;
                    } else {
                        if (Math.abs(tanDyx) < rectDyx && upX == 1) {
                            connPointY = fromRectCenterPoint.y + fromRect.width / 2 * tanDyx;
                            connPointX = fromRectCenterPoint.x + fromRect.width / 2;
                        }
                    }
                }
            }
            return {
                x : connPointX,
                y : connPointY
            };
        }
    };

})(jQuery);
